package com.zhuss.core.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.config.DeleteConfigBo;
import com.zhuss.bean.vo.core.config.DetailConfigVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.ConfigDao;
import com.zhuss.core.db.entity.Config;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.OceanException;
import com.zhuss.util.JsonUtil;

/**
 * 删除配置 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/20 21:55
 */
@Component("configDelete")
public class ConfigDelete extends AbstractProcessor<Object, DeleteConfigBo> implements Processor<Object, DeleteConfigBo> {

    @Autowired
    private ConfigDao configDao;
    
    @Autowired
	@Qualifier("configRead")
	private ConfigRead configRead;
    
    /**
     * 校验
     */
	@Override
    public void check(DeleteConfigBo bo) throws OceanException {
    	// Long configId = bo.getConfigId();
    	// throw new OceanException(ExceptionCode.不能删除业务的配置);
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(DeleteConfigBo param) throws OceanException {
    	Long configId = param.getConfigId();
    	String value = ((DetailConfigVo) configRead.detail(configId)).getValue();
    	
    	Config record = new Config();
    	record.setConfigId(configId);
    	record.setDeleted(param.getDeleted());
    	
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	configDao.delete(record);
    	param.setName(value);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(DeleteConfigBo param) throws OceanException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.配置删除.key());
    	record.setTargetId(param.getConfigId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
